package com.example.administrator.mywechat;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.widget.RadioGroup;

import com.example.administrator.mywechat.adapter.MyFragmentAdapter;
import com.example.administrator.mywechat.fragment.DongtaiFragment;
import com.example.administrator.mywechat.fragment.MsgFragment;
import com.example.administrator.mywechat.fragment.PersonFragment;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity {
    private MsgFragment mMsgFragment;
    private PersonFragment mPersonFragment;
    private DongtaiFragment mDongtaiFragment;

    private RadioGroup mRadioGroup;
    private FragmentManager fm;
    private List<Fragment> mFragments;
    private ViewPager mViewPager;
    private FragmentTransaction transaction;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mRadioGroup = (RadioGroup) findViewById(R.id.radiogroup);
        mViewPager = (ViewPager) findViewById(R.id.viewpager_container);

        fm = getSupportFragmentManager();
        mMsgFragment = new MsgFragment();
        mPersonFragment = new PersonFragment();
        mDongtaiFragment = new DongtaiFragment();
        mFragments = new ArrayList<>();
        mFragments.add(mMsgFragment);
        mFragments.add(mPersonFragment);
        mFragments.add(mDongtaiFragment);
        MyFragmentAdapter adapter = new MyFragmentAdapter(fm, mFragments);
        mViewPager.setAdapter(adapter);
        /**
         * 设置底部的radiobutton的选中状态
         */
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        mRadioGroup.check(R.id.radiobutton_msg);
                        break;
                    case 1:
                        mRadioGroup.check(R.id.radiobutton_person);
                        break;
                    case 2:
                        mRadioGroup.check(R.id.radiobutton_dongtai);
                        break;
                    default:
                        break;
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        /**
         * 当选中的radiobutton发生变化的时候fragment也跟着发生变化
         */
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                transaction = fm.beginTransaction();
                switch (checkedId) {
                    case R.id.radiobutton_msg:
                        mViewPager.setCurrentItem(0);
                        break;
                    case R.id.radiobutton_person:
                        mViewPager.setCurrentItem(1);
                        break;
                    case R.id.radiobutton_dongtai:
                        mViewPager.setCurrentItem(2);
                        break;
                    default:
                        break;
                }
                transaction.commit();
            }
        });
    }
}
